function request(url, params, method = 'GET') {
  // console.log(url, params);
  let header = null
  let timeout = 10000
  let tokenName = 'key'
  let p = new Promise((resolve, reject) => {
    // 开启login
    wx.showNavigationBarLoading()
    let token = wx.getStorageSync(tokenName)
    // params.key = token

    // 请求
    wx.request({
      url: url,
      data: params,
      timeout,
      header: header || {
        'Content-Type': 'application/json'
      },
      method,
      success: (res) => {
        switch (res.statusCode) {
          case 200:
            resolve(res.data)
            break;
          case 500:
            resolve(res.data)
            break;
          case 404:
            resolve(res.data)
            break;
          case 401:
            resolve(res.data)
            break;
          case 403:
            resolve(res.data)
            break;
          default:
            wx.showToast({
              title: '出了点错误！请检查',
              icon: 'none'
            })
            reject(res)
            break;
        }
      },
      fail: err => {
        reject(err)
      },
      complete: () => {
        wx.hideNavigationBarLoading()
      }

    })
  })
  return p
}

function get(url, params = {}, method = 'GET') {
  return request(url, params, method)
}

function post(url, params = {}, method = 'POST') {
  return request(url, params, method)
}

function http(url, params, method = 'GET') {
  return request(url, params, method)
}
http.get = get
http.post = post


export default http